package com.ciwong.epaper.modules.evaluate.ui;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.evaluate.bean.SentencesDetail;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.download.DownLoadInfo;

import java.util.ArrayList;

/**
 * Created by lzr on 2017/4/17.
 * 全文跟读
 */
public class SentenceAllActivity extends SentenceRepeatActivity {

    @Override
    protected void preCreate() {
        //获取数据
        Intent intent = getIntent();
        if (intent != null) {
            sentencesDetails = (ArrayList<SentencesDetail>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_SENTENCES_LIST);
            mModule = (Module) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
            position = intent.getIntExtra(IntentFlag.INTENT_FLAG_POSITION, 0);
            mDownLoadInfo = (DownLoadInfo) intent
                    .getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
        }
    }

    @Override
    protected void init() {
        if (mDownLoadInfo != null && mDownLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {
            setTitleText(mDownLoadInfo.getResourceName());
        } else {
            setTitleText(mModule.getResourceList().get(position).getResourceName());
        }
        isInSentenceAll = true;

    }

    @Override
    protected void findViews() {
        super.findViews();
        setBackImg(R.mipmap.searbook_shut_down);
        sumView.setVisibility(View.VISIBLE);
        playAllView.setVisibility(View.GONE);
        mRlCheckMode.setVisibility(View.GONE);
    }

    @Override
    protected void loadData() {
        //全文跟读不重新加载数据
        initYzs();
        refreshSentencesUi(null);
        startImg.setImageResource(R.mipmap.type_time_out);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                playImg();
            }
        }, 400);


    }

    @Override
    protected void initYzs() {
        speechPlayController = new SpeechAllPlayController(speechView);
        speechPlayController.setSpeechPlayInterFace(this);
    }

    @Override
    protected void playImg() {
        //添加一个空白控件其实就是为了能够滚动
        try {
            LinearLayout linearLayout = new LinearLayout(this);
            int lastItemHei = speechView.getContainer().getChildAt(speechView.getContainer().getChildCount() - 1).getHeight();
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, speechView.getHeight() - lastItemHei);
            speechView.getContainer().addView(linearLayout, lp);
            if (speechPlayController.getCurrentListenAndRepeatIndex() == 0) {
                setNo(0, sentencesDetails.size());
            }
            startPlay();
        } catch (NullPointerException e) {
            e.getStackTrace();
        }

    }

    @Override
    protected void stopImg(boolean isFinish) {
        try {
            super.stopImg(isFinish);
        } catch (NullPointerException e) {
            e.getStackTrace();
        }

    }

    @Override
    public void nextPlay(int currentIndex, int count, boolean isPrevious) {
        super.nextPlay(currentIndex, count, isPrevious);
        if (currentIndex >= count) {
            stopImg(false);
        }
    }

    @Override
    protected void bindAfData() {
    }

    @Override
    protected boolean isBack() {
        return !super.isBack();
    }

    @Override
    protected boolean isNeedSaveAn() {
        return !super.isNeedSaveAn();
    }

    @Override
    protected void addSentenceToView(int currIndex, boolean isPlayState, boolean isNeedReset, boolean isFromPlayAll, boolean isPlayAll) {
        speechView.addSentenceToView(currIndex, isPlayState);
    }

    @Override
    protected void refreshSpeechView(boolean isState) {
        speechPlayController.refreshAllSpeechView(false);
    }


}
